//
//  MZProgramDetailDesignedViewController.m
//  Mehfile Zainab
//
//  Created by Badourali Azad on 08/02/2014.
//  Copyright (c) 2014 Azad Badourali. All rights reserved.
//

#import "MZProgramDetailDesignedViewController.h"

@interface MZProgramDetailDesignedViewController ()

@end

@implementation MZProgramDetailDesignedViewController

@synthesize programItem, eventStore, defaultCalendar;

@synthesize dateLabel, titleLabel, timeLabel, contentView, calendarButton;

/*
 ------------------------------------------------------------------------
 UIViewController methods
 ------------------------------------------------------------------------
 */

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	
	// Initialize an event store object with the init method. Initilize the array for events.
	self.eventStore = [[EKEventStore alloc] init];
    
    if([self.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            /* This code will run when uses has made his/her choice */
        }];
    }
    
	// Get the default calendar from store.
	self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
	
	self.dateLabel.textColor = [self colorFromHexString:@"#C22809"];
    self.dateLabel.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:25.0f];
	self.dateLabel.text = [programItem getProgramDate];
	
	self.titleLabel.textColor = [self colorFromHexString:@"#173A1F"];
    self.titleLabel.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:25.0f];
	self.titleLabel.text = [programItem getProgramTitle];
	
	//self.timeLabel.textColor = [self colorFromHexString:@"#045905"];
    self.timeLabel.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:25.0f];
	self.timeLabel.text = [programItem getProgramStartTime];
	
	self.contentView.editable = FALSE;
    self.contentView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber; //UIDataDetectorTypeAll;
    self.contentView.textColor = [self colorFromHexString:@"#bcc2c8"];
    self.contentView.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:24.0f];
	self.contentView.text = [programItem getProgramDescription];
	
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 ------------------------------------------------------------------------
 EKEventEditViewDelegate protocol methods
 ------------------------------------------------------------------------
 */

// Overriding EKEventEditViewDelegate method to update event store according to user actions.
- (void)eventEditViewController:(EKEventEditViewController *)controller
          didCompleteWithAction:(EKEventEditViewAction)action {
	
	NSError *error = nil;
	EKEvent *thisEvent = controller.event;
	
	switch (action) {
		case EKEventEditViewActionCanceled:
			// Edit action canceled, do nothing.
			break;
			
		case EKEventEditViewActionSaved:
			// When user hit "Done" button, save the newly created event to the event store,
			// and reload table view.
			// If the new event is being added to the default calendar, then update its
			// eventsList.
			[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Mehfile Zainab" message:@"Evènement ajouté à votre calendrier" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
			break;
			
		case EKEventEditViewActionDeleted:
			// When deleting an event, remove the event from the event store,
			// and reload table view.
			// If deleting an event from the currenly default calendar, then update its
			// eventsList.
			[controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error];
			break;
			
		default:
			break;
	}
	// Dismiss the modal view controller
	//[controller dismissModalViewControllerAnimated:YES];
	[controller dismissViewControllerAnimated:YES completion:nil];
    
}

// Set the calendar edited by EKEventEditViewController to our chosen calendar - the default calendar.
- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller {
	EKCalendar *calendarForEdit = self.defaultCalendar;
	return calendarForEdit;
}

/*
 ------------------------------------------------------------------------
 Custom methods
 ------------------------------------------------------------------------
 */

- (UIColor *)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; // bypass '#' character
    [scanner scanHexInt:&rgbValue];
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}

-(IBAction) addAgenda {
	
	NSLog(@"Add event to Calendar");
    
    // When add button is pushed, create an EKEventEditViewController to display the event.
	EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil];
	
	// set the addController's event store to the current event store.
	addController.eventStore = self.eventStore;
    
    EKEvent *thisEvent = addController.event;
    
    [thisEvent setTitle:programItem.getProgramTitle];
    
    NSString *addrName = @"Mehfile Zainab";
    NSString *addrValue = @"19, avenue Gabriel Péri";
    NSString *addrCity = @"93120 La Courneuve";
    NSString *addrLocation = [NSString stringWithFormat:@"%@\r%@\r%@", addrName,addrValue,addrCity];
    [thisEvent setLocation:addrLocation];
	
    NSURL *mzUrl = [[NSURL alloc] initWithString:@"http://www.mehfile-zainab.com"];
    [thisEvent setURL:mzUrl];
    
    NSString *startProgramDate = [NSString stringWithFormat:@"%@ %@", programItem.mzProgramDate, programItem.mzProgramStartTime];
    NSString *endProgramDate = [NSString stringWithFormat:@"%@ %@", programItem.mzProgramDate, programItem.mzProgramEndTime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease]];
    [dateFormatter setDateFormat:@"eeee dd MMMM yyyy HH'h'mm"];
    NSDate *startDateFromString = [[NSDate alloc] init];
    startDateFromString = [dateFormatter dateFromString:startProgramDate];
    [thisEvent setStartDate:startDateFromString];
    NSDate *endDateFromString = [[NSDate alloc] init];
    endDateFromString = [dateFormatter dateFromString:endProgramDate];
    [thisEvent setEndDate:endDateFromString];
	
	// present EventsAddViewController as a modal view controller
	//[self presentModalViewController:addController animated:YES];
	[self presentViewController:addController animated:YES completion:nil];
    
	addController.editViewDelegate = self;
	[addController release];
	
}

@end
